$(document).ready(function(){
    var _self = $('#SELF').val();
    var _user_type_json = eval('('+$("#user_type_json").val()+')');
    $("#user_list").jqGrid({
        url:_self+'/a/get_user_list',//获取用户列表URL
        editurl:_self+'/a/opt_user_data',//增加、修改、删除URL
        mtype:'POST',//
        datatype: "json",//返回数据格式
        pager : '#user_list_footer',//表格底部导航栏ID

        height:'100%',//高度（100%为自适应）
        width:'800',//宽度

        viewrecords:true,//是否要显示总记录数
        multiselect: true,//多选
        
        rowNum : 10,//每页行数
        rowList : [5,10,20,30],//可选择的每页行数
        
        jsonReader : {//配置返回的数据格式
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems: false,
            cell: "",
            id: "0",
            userdata: "userdata",
            subgrid: {
                root:"rows",
                repeatitems: false,
                cell:"cell"
            }
        },
        colNames:['ID','用户名', '密码', 'E-mail','用户类型','状态'],//表头
        colModel:[ {
                name:'id',
                index:'id',
                width:55,
                editable: false
                //editoptions:{readonly:true} //只读
            }, {
                name:'username',
                index:'username',
                width:90,
                editable: true
            }, {
                name:'password',
                index:'password',
                width:100,
                editable: true,
                formatter:passwordFmatter//回调函数设置显示的格式
            }, {
                name:'email',
                index:'email',
                width:80,
                align:"left",
                editable: true
            }, {
                name:'usertype',
                index:'usertype',
                width:80,
                align:"center",
                editable: true,
                edittype:"select",//编辑时为SELECT下拉框
                editoptions:{value:usertype2str()},//下拉框的值
                formatter:userTypeFmatter
            }, {
                name:'status',
                index:'status',
                width:80,
                align:"center",
                editable: true,
                edittype:"select",editoptions:{value:"1:启用;0:禁用"},
                formatter:statusFmatter
            }]
    });
    $("#user_list").jqGrid('navGrid','#user_list_footer',{
        edit:true,
        add:true,
        del:true
    });

    function usertype2str(){
        var str = '';
        for(key in _user_type_json)
            str += (str == '' ? '' : ';') + key + ':' + _user_type_json[key];
        return str;
    }
    function passwordFmatter (cellvalue, options, rowObject ){
        return '******';
    }
    function userTypeFmatter (cellvalue, options, rowObject ){
        return _user_type_json[cellvalue];
    }
    function statusFmatter (cellvalue, options, rowObject ){
        return cellvalue == '1' ? '<font color="green">启用</font>' : '<font color="red">禁用</font>';
    }

});